<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/12/20 0020
  Time: 8:30
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
</head>
<body>
<form action="Index.jsp">
    <div id="nameDiv"> </div>
    用户名:<input type="text" id="username" name="username" onblur="CheckUsername()" required><div style="display: inline" id="usernameDiv"></div><br/>
    密码:<input type="text" id="password" name="password" onblur="CheckPassword()" required><div style="display: inline" id="passwordDiv"></div><br/>
    确认密码:<input type="text" id="repassword" name="repassword" onblur="CheckRePassword()" required><div style="display: inline" id="repasswordDiv"></div><br/>
    <input type="submit" onclick="" value="注册">
    <input type="reset" value="重置"><br/>
    <a href="Login.jsp">返回首页</a>
</form>

<script src="/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
    function CheckUsername(){
        var username=$("#username").val();
        if(username==null||username==""){
            $("#usernameDiv").html("用户名不能为空！");
            return;
        }else{
            $("#usernameDiv").html("");
        }
        xmlHttpRequest = CreateXMLHttpRequest();
        xmlHttpRequest.onreadystatechange=callBack;
        var url="/RegisterHTTPServlet?username="+username;
        xmlHttpRequest.open("get",url,true);
        xmlHttpRequest.send(null);
    }
    function CheckPassword() {
        var password = $("#password").val();
        if (password == null || password == "") {
            $("#passwordDiv").html("密码不能为空！");
            return;
        }else{
            $("#passwordDiv").html("");
        }
        var repassword=$("#repassword").val();
        if(repassword!=password){
            $("#repasswordDiv").html("两次输入密码不一样！");
            return;
        }else{
            $("#repasswordDiv").html("");
        }
    }

    function CheckRePassword(){
        var repassword=$("#repassword").val();
        if(repassword==null||repassword==""){
            $("#repasswordDiv").html("两次输入密码不一样");
            return;
        }else{
            $("#repasswordDiv").html("");
        }
        var password=$("#password").val();
        if(repassword!=password){
            $("#repasswordDiv").html("两次输入密码不一样！");
           return;
        }else{
            $("#repasswordDiv").html("");
        }
    }

    function callBack(){
        if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
            var data = xmlHttpRequest.responseText;
            if (data == "true") {
                $("#nameDiv").html("用户名可以使用！");
            } else {
                $("#nameDiv").html("用户名已被使用！");
            }
        }
    }

    function CreateXMLHttpRequest(){
        if(window.XMLHttpRequest){
            return new XMLHttpRequest();
        }
    }
</script>
</body>
</html>
